var jCategoryTree;
$j(document).ready(function () {
	//验证
	applyValidate($("lowestPrice"),"price");
	applyValidate($("highestPrice"),"price");
	
});


//显示操作按钮
function displayListButtons(status){
	enableIconBtn('multiAdjust');
}	
//点击树节点动作
function clickTreeItemAction(){
	var treeItem=getCategoryItemJSON(jCategoryTree.getSelected());
	searchProducts(true,{'categoryPath':treeItem.categoryPath});
}

//编辑库存
function showAuditLog(productSkuId){
	showDialogOfinventoryAuditListSelector(productSkuId);
}

//在本页以ajaxPage方式打开inventoryForm
function showInventoryForm(url,param){
	$j('#mainDiv').hide();
	$j('#formDiv').show().loading().load(url,param);
}

//刷新产品列表
function refreshProductList(){
	searchProducts(false);
}
//分页
function fnOnGoToPage(){
	refreshProductList();
}

//对已有结果进行排序,ascValue表示升序时对应的值,descValue表示降序时对应的值
function sortField(linkDOM,ascValue,descValue){
	var jThDOM = $j(linkDOM).parent('th');
	if(jThDOM.hasClass("order1")){
		$j('#storageOrderby').val(descValue);
	}else{
		$j('#storageOrderby').val(ascValue);
	}
	searchProducts(false);
}

//搜索产品 isNew标示是否新的搜索,param标示是否自定义参数搜索
function searchProducts(isNew,param){
	var postData;
	var url;
	if(isNew){
		if(null==param){
			if(validateSearchCriteria()){
				//有搜索结果搜索
			    postData = $j('#productSearchCriteria :input').serializeArray();
			    url=__ctxPath+"/inventory/inventory/blank.html?doAction=search&btnSearch=1";
				$j("#productResults").loading().load(url,postData);
		    }else{
		    	return;
		    }
		}else{
			//新的搜索。全部，上架等
			//有搜索结果搜索
			if(param.inventoryStatus  != undefined){
				$j('#inventoryStatus').val(""+param.inventoryStatus);
			}
			if(param.status  != undefined){
				$j('#storageStatus').val(""+param.status);
			}
			if(param.categoryId  != undefined){
				$j('#storageCategoryId').val(""+param.categoryId);
			}
			if(param.categoryPath  != undefined){
				$j('#storageCategoryPath').val(""+param.categoryPath);
			}
		    postData = $j('#productResults :input').serializeArray();
		    url=__ctxPath+"/inventory/inventory/blank.html?doAction=search&btnSearch=1";
			$j("#productResults").loading().load(url,postData);  
		}    
	}else{
		postData = $j('#productResults :input').serializeArray();
		url=__ctxPath+"/inventory/inventory/blank.html?&doAction=search";
		$j("#productResults").loading().load(url,postData);  
	} 
	
}


//验证函数
function validateSearchCriteria(){
	if(!validateForm($j('#inventorySearchCriteria').get(0))){
    	return false;
   	}
   	return true;
}

//重设搜索条件
function resetSearchCriteria(){
	$("sc_inventoryStatus").selectedIndex = 0;
	$("sc_keywords").value = "";
	$("sc_excludedKeywords").value = "";
	$("sc_productCode").value = "";
	$("lowestPrice").value = "";
	$("highestPrice").value = "";
	$("sc_categoryPaths").selectedIndex = -1;
	$("sc_brandIds").selectedIndex = -1;
	$("sc_productTypeIds").selectedIndex = -1;
	$("sc_status").selectedIndex = 0;
	$("sc_orderby").selectedIndex = 0;
}


//更新可售库存 
function updateOnhand(event){
	$j(this).parent("li").find("font").remove();
	if(event.originalEvent.type=="keyup" && event.keyCode != 13){
		return;
	}

	var valStr = "";
	if ($j(this).val() && event.data.allocatedQ && $j(this).val()<event.data.allocatedQ) {
		valStr = "minValue="+event.data.allocatedQ+",intRange=[0-100000]";
	} else {
		valStr = "intRange=[0-100000]";
	}
	
	applyValidate(this,valStr);
	if(!validateForm($('productListForm'))){
		$j(this).val(event.data.quantityOh);
		removeValidate(this);
		return false;
	}else{
		removeValidate(this);
		var url=__ctxPath+"/inventory/inventory/blank.html?doAction=updateOnhand";
		$j('#inventoryInfo_'+event.data.productSkuId).loading().load(url,{"productSkuId":event.data.productSkuId,"onhand":$j(this).val()});
	}	
	
}

//更新低库存警告数量 
function updateReorderMinimnm(event){
	if(event.originalEvent.type=="keyup" && event.keyCode != 13){
		return;
	}
	applyValidate(this,"required,intRange=[0-100000]");
	if(!validateForm($('productListForm'))){
		removeValidate(this);
		return false;
	}else{
		removeValidate(this);
		var url=__ctxPath+"/inventory/inventory/blank.html?doAction=updateReorderMinimnm";
		$j('#inventoryInfo_'+event.data.productSkuId).loading().load(url,{"productSkuId":event.data.productSkuId,"reorderMinimnm":$j(this).val()});
	}	
	
}


